package org.example.admin.flight;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class addFlightView extends JFrame {
    private JTextField flightIdField,flightNumField,airwayIdField,departureTimeField,arrivalTimeField,aircraftIdField,priceField,availableSeatField;
    private JButton addButton;

    public addFlightView() {
        setTitle("管理端航班信息添加页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(9, 2));

        JLabel flightIdLabel = new JLabel("航班id");
        flightIdField = new JTextField();

        JLabel flightNumLabel = new JLabel("航班数量");
        flightNumField = new JTextField();

        JLabel airwayIdLabel = new JLabel("航线id");
        airwayIdField = new JTextField();

        JLabel departureTimeLabel = new JLabel("出发时间");
        departureTimeField = new JTextField();

        JLabel  arrivalTimeLabel = new JLabel("到达时间");
        arrivalTimeField = new JTextField();

        JLabel  aircraftIdLabel = new JLabel("飞机id");
        aircraftIdField = new JTextField();

        JLabel  priceLabel = new JLabel("票价");
        priceField = new JTextField();

        JLabel  availableSeatLabel = new JLabel("可预订座位");
        availableSeatField = new JTextField();


        addButton = new JButton("添加");

        // 添加组件到窗口00
        add(flightIdLabel);
        add(flightIdField);

        add(flightNumLabel);
        add(flightNumField);

        add(airwayIdLabel);
        add(airwayIdField);

        add(departureTimeLabel);
        add(departureTimeField);

        add(arrivalTimeLabel);
        add(arrivalTimeField);

        add(aircraftIdLabel);
        add(aircraftIdField);

        add(priceLabel);
        add(priceField);

        add(availableSeatLabel);
        add(availableSeatField);

        add(new JLabel()); // 占位
        add(addButton);

        // 添加添加按钮的点击事件监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addUserInfoToDatabase();
            }
        });
    }

    private void addUserInfoToDatabase() {
        String flightId = flightIdField.getText();
        String flightNum = flightNumField.getText();
        String airwayId = airwayIdField.getText();
        String departureTime = departureTimeField.getText();
        String arrivalTime = arrivalTimeField.getText();
        String aircraftId = aircraftIdField.getText();
        String price =priceField.getText();
        String availableSeat =availableSeatField.getText();

        try {
            // 连接数据库
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");

            // 创建插入语句
            String insertQuery = "INSERT INTO air_flight (flight_id, flight_num, airway_id, departure_time,arrival_time,aircraft_id,price,available_seat) VALUES (?, ?, ?, ? ,?, ?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(insertQuery);
            statement.setString(1, flightId);
            statement.setString(2, flightNum);
            statement.setString(3, airwayId);
            statement.setString(4, departureTime);
            statement.setString(5, arrivalTime);
            statement.setString(6, aircraftId);
            statement.setString(7, price);
            statement.setString(8, availableSeat);

            // 执行插入操作
            statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            conn.close();

            // 提示添加成功
            JOptionPane.showMessageDialog(this, "添加成功");

            // 清空输入框
            clearFields();

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "添加失败：" + e.getMessage());
        }
    }

    private void clearFields() {
        flightIdField.setText("");
        flightNumField.setText("");
        airwayIdField.setText("");
        departureTimeField.setText("");
        arrivalTimeField.setText("");
        aircraftIdField.setText("");
        priceField.setText("");
        availableSeatField.setText("");
    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            addFlightView addFlightForm = new addFlightView();
//            addFlightForm.setSize(300, 200);
//            addFlightForm.setVisible(true);
//        });
    }
}